// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CombatComponent from "../../Combats/CombatComponent";
import GameEvent from "../../Commons/GameEvent";
import { ResPath } from "../../Enums/GameEnum";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import CharacterEntity from "../../Models/Characters/CharacterEntity";
import CharacterManager from "../../Models/Characters/CharacterManager";
import CharacterTemplate from "../../Models/Characters/CharacterTemplate";
import LevelManager from "../../Models/Levels/LevelManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HeroItemUi extends cc.Component {
    @property(cc.Sprite)
    icon: cc.Sprite = null;
    @property(cc.Label)
    lvTxt: cc.Label = null;
    @property(cc.Label)
    ackTxt: cc.Label = null;
    @property(cc.Label)
    costTxt: cc.Label = null;
    @property(cc.Label)
    hpTxt: cc.Label = null;
    @property(cc.Button)
    unlockBtn: cc.Button;
    @property(cc.Button)
    upgradeBtn: cc.Button = null;
    @property(cc.Label)
    moveSpeedTxt: cc.Label = null;

    characterTp: CharacterTemplate;
    characterEy: CharacterEntity;
    countTime: number = 0;

    protected onEnable(): void {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    protected onDisable(): void {
        // this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    onTouchStart() {

    }

    drawItem(tp: CharacterTemplate) {
        this.characterTp = tp;
        this.characterEy = CharacterManager.single.getCharacteEntity(tp.id);
        if (this.characterEy != null) {
            this.hpTxt.string = this.characterEy.health + "";
            this.lvTxt.string = "" + this.characterEy.lv;
            this.ackTxt.string = "" + Math.floor(this.characterEy.attack);
            this.unlockBtn.node.active = false;
            this.upgradeBtn.node.active = true;
            this.costTxt.string = this.characterEy.upgradeCost + "";
            this.moveSpeedTxt.string = this.characterEy.moveSpeed.toFixed(1);
        } else {
            this.unlockBtn.node.active = true;
            this.upgradeBtn.node.active = false;

            this.hpTxt.string = "?";
            this.lvTxt.string = "?";
            this.ackTxt.string = "?";
            this.moveSpeedTxt.string = "?";
        }


        cc.resources.load(ResPath.icon_path + this.characterTp.id, cc.SpriteFrame, (err: Error, spriteFrame: cc.SpriteFrame) => {
            this.icon.spriteFrame = spriteFrame;
        });
    }

    upgradeClick() {
        CharacterManager.single.requestUpLevel(this.characterEy.id);
        if (SaveManager.single.userdata.guideId == 2) {
            SaveManager.single.updataGuideId();
            var guideHand = this.node.getChildByName("GuideHand");
            if (guideHand != null) {
                guideHand.removeFromParent();
                guideHand.active = false;
                guideHand.destroy();
            }
        }
    }

    unlockClick() {
        if (this.characterTp.id == 3006) {
            UIManager.single.showTips("购买礼包获得。");
            return;
        }

        if (this.characterTp.id == 3003 || this.characterTp.id == 3004 || this.characterTp.id == 3005) {
            var needDiamod = 0;
            if (this.characterTp.id == 3003) {
                needDiamod = 500;
            } else if (this.characterTp.id == 3004) {
                needDiamod = 1000;
            } else {
                needDiamod = 2000;
            }

            UIManager.single.showDialog(`是否消耗${needDiamod}钻石，解锁英雄。`, () => {

                if (!SaveManager.single.isEnoughDiamond(needDiamod)) {
                    UIManager.single.showTips("钻石不足。");
                    return;
                }
                var usedata = SaveManager.single.userdata;
                usedata.diamond -= needDiamod;
                SaveManager.single.saveData();
                CharacterManager.single.requestUnlockHero(this.characterTp.id);
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
            }, null, this, true);
        } else {
            var id = CharacterManager.single.getUnlockGate(this.characterTp.id);
            UIManager.single.showTips(`通关<关卡${id}>解锁。`);
        }
    }


    protected update(dt: number): void {
        // return;
        if (this.characterEy == null) {
            return;
        }
        if (!CombatComponent.startCombat) {
            return;
        }
        if (CC_DEBUG) {
            // if (CharacterManager.test) {
            //     return;
            // }
        }

        this.countTime += dt * (SaveManager.single.userdata.addSpeedMode + 1);
        if (this.countTime * 1000 >= this.characterEy.combatTime) {
            CharacterManager.test = true;
            this.countTime = 0;
            GameEvent.eventNode.emit(GameEvent.MAKECHARACTER_EVENT, this.characterEy);
        }
    }

}
